home *** CD-ROM | disk | FTP | other *** search
/ Oh!X 2000 Spring / Oh!X 2000 Spring Special CD-ROM (Japan) (Part 2).7z / Oh!X 2000 Spring Special CD-ROM (Japan) (Part 2).bin / DXF / samples / multimedia / dplay / src / dpslots / dialog.cpp < prev    next >
C/C++ Source or Header  |  1999-07-15  |  34KB  |  1,046 lines

  1. //-----------------------------------------------------------------------------
  2. // File: dialog.cpp
  3. //
  4. // Desc: Creates a dialog to query the user for connection settings
  5. //       and establish a connection.
  6. //
  7. // Copyright (C) 1996-1999 Microsoft Corporation.  All Rights Reserved.
  8. //-----------------------------------------------------------------------------
  9. #define INITGUID
  10. #include <windows.h>
  11. #include <windowsx.h>
  12. #include <wtypes.h>
  13. #include <cguid.h>
  14.  
  15. #include <dplobby.h>    // to init guids
  16. #include "dpslots.h"
  17. #include "resource.h"
  18.  
  19. // constants
  20. const UINT      TIMERID         = 1;            // timer ID to use
  21. const UINT      TIMERINTERVAL   = 1000;         // timer interval
  22.  
  23. // structures
  24.  
  25. // server configuration information
  26. struct SERVERCONFIG
  27. {
  28.     CHAR strServerName[MAXSTRLEN];
  29.     CHAR strDatabaseFile[MAXSTRLEN];
  30.     CHAR strSecurityProvider[MAXSTRLEN];
  31.     BOOL bRequireSecureLogin;
  32. };
  33.  
  34. // client login information
  35. struct CLIENTLOGIN
  36. {
  37.     CHAR strPlayerName[MAXSTRLEN];
  38.     CHAR strUserName[MAXSTRLEN];
  39.     CHAR strPassword[MAXSTRLEN];
  40.     CHAR strDomain[MAXSTRLEN];
  41. };
  42.  
  43. // prototypes
  44. BOOL FAR PASCAL DirectPlayEnumConnectionsCallback( const GUID* pSPGUID,
  45.                         VOID* pConnection, DWORD dwConnectionSize,
  46.                         const DPNAME* pName, DWORD dwFlags, VOID* pContext );
  47. BOOL CALLBACK   ConnectWndProc( HWND hWnd, UINT uMsg, WPARAM wParam,
  48.                                 LPARAM lParam );
  49. HRESULT CreateDirectPlayInterface( LPDIRECTPLAY4A* ppDPlay );
  50. HRESULT DestroyDirectPlayInterface( HWND hWnd, LPDIRECTPLAY4A pDPlay );
  51. HRESULT HostSession( LPDIRECTPLAY4A pDPlay, SERVERCONFIG* pServerConfig,
  52.                      DPLAYINFO* pDPInfo );
  53. HRESULT JoinSession( LPDIRECTPLAY4A pDPlay, GUID* pSessionInstanceGUID,
  54.                      CLIENTLOGIN* pClientLogin, DPLAYINFO* pDPInfo );
  55. HRESULT EnumSessions( HWND hWnd, LPDIRECTPLAY4A pDPlay );
  56. HRESULT GetConnection( HWND hWnd, VOID** ppConnection );
  57. VOID    DeleteConnectionList( HWND hWnd );
  58. HRESULT GetSessionInstanceGuid( HWND hWnd, GUID* pguidSessionInstanceGUID );
  59. VOID    SelectSessionInstance( HWND hWnd, GUID* pguidSessionInstanceGUID );
  60. VOID    DeleteSessionInstanceList( HWND hWnd);
  61. BOOL    GetServerConfig( HWND hWnd, SERVERCONFIG* pServerConfig );
  62. BOOL    GetClientLogin( HWND hWnd, CLIENTLOGIN* pClientLogin );
  63. HRESULT GetSessionDesc( LPDIRECTPLAY4A pDPlay, DPSESSIONDESC2** ppSessionDesc );
  64.  
  65.  
  66.  
  67.  
  68. //-----------------------------------------------------------------------------
  69. // Name: ConnectUsingDialog()
  70. // Desc:
  71. //-----------------------------------------------------------------------------
  72. HRESULT ConnectUsingDialog( HINSTANCE hInstance, DPLAYINFO* pDPInfo )
  73. {
  74.     // Ask user for connection settings
  75.     if( DialogBoxParam( hInstance, MAKEINTRESOURCE(IDD_CONNECTDIALOG),
  76.                         NULL, (DLGPROC)ConnectWndProc, (LPARAM)pDPInfo ) )
  77.         return DP_OK;
  78.     else
  79.         return DPERR_USERCANCEL;
  80. }
  81.  
  82.  
  83.  
  84.  
  85. //-----------------------------------------------------------------------------
  86. // Name: ConnectWndProc()
  87. // Desc:
  88. //-----------------------------------------------------------------------------
  89. BOOL CALLBACK ConnectWndProc( HWND hWnd, UINT uMsg, WPARAM wParam,
  90.                               LPARAM lParam )
  91. {
  92.     static DPLAYINFO*     pDPInfo;
  93.     static LPDIRECTPLAY4A pDPlay;
  94.     static UINT           idTimer = 0;
  95.     GUID         guidSessionInstance;
  96.     SERVERCONFIG serverConfig;
  97.     CLIENTLOGIN  clientLogin;
  98.     VOID*        pConnection = NULL;
  99.     DWORD        dwNameSize;
  100.     HRESULT      hr;
  101.  
  102.     switch( uMsg )
  103.     {
  104.         case WM_INITDIALOG:
  105.             // Save the connection info pointer
  106.             pDPInfo = (DPLAYINFO*)lParam;
  107.             pDPlay  = NULL;
  108.  
  109.             // Create an IDirectPlay3 interface
  110.             hr = CreateDirectPlayInterface( &pDPlay );
  111.  
  112.             if( FAILED(hr) )
  113.                 goto SETUP_FAILURE;
  114.  
  115.             // Set first item in the connections combo box
  116.             SendDlgItemMessage( hWnd, IDC_SPCOMBO, CB_ADDSTRING, 0,
  117.                                 (LPARAM)"<Select a service provider>");
  118.             SendDlgItemMessage( hWnd, IDC_SPCOMBO, CB_SETITEMDATA, 0, 0 );
  119.             SendDlgItemMessage( hWnd, IDC_SPCOMBO, CB_SETCURSEL, 0, 0 );
  120.  
  121.             // Put all the available connections in a combo box
  122.             pDPlay->EnumConnections( &DPSLOTS_GUID,
  123.                                      DirectPlayEnumConnectionsCallback,
  124.                                      hWnd, 0 );
  125.  
  126.             // Setup initial button state
  127.             EnableDlgButton( hWnd, IDC_HOSTBUTTON, FALSE );
  128.             EnableDlgButton( hWnd, IDC_JOINBUTTON, FALSE );
  129.             break;
  130.     
  131.     SETUP_FAILURE:
  132.             ErrorBox("Could not create DirectPlay object because of error %s", hr);
  133.             EndDialog( hWnd, FALSE );
  134.             break;
  135.  
  136.         case WM_DESTROY:
  137.             // delete information stored along with the lists
  138.             DeleteConnectionList( hWnd );
  139.             DeleteSessionInstanceList( hWnd );
  140.             break;
  141.  
  142.         case WM_TIMER:
  143.             // Refresh the session list
  144.             // Guard against leftover timer messages after timer has been killed
  145.             if( idTimer )
  146.             {
  147.                 hr = EnumSessions( hWnd, pDPlay );
  148.                 if( FAILED(hr) && hr != DPERR_CONNECTING )
  149.                 {
  150.                     KillTimer( hWnd, idTimer );
  151.                     idTimer = 0;
  152.                     if( hr != DPERR_USERCANCEL )
  153.                         ErrorBox("Enumerating sessions has failed; error %s", hr);
  154.                 }
  155.             }
  156.             break;
  157.  
  158.         case WM_COMMAND:
  159.             switch(LOWORD(wParam))
  160.             {
  161.                 case IDC_SPCOMBO:
  162.                     switch (HIWORD(wParam))
  163.                     {
  164.                         case CBN_SELCHANGE:
  165.                             // Service provider changed, so rebuild display and
  166.                             // delete any existing DirectPlay interface
  167.                             if( idTimer )
  168.                             {
  169.                                 KillTimer( hWnd, idTimer );
  170.                                 idTimer = 0;
  171.                             }
  172.                             hr = DestroyDirectPlayInterface( hWnd, pDPlay );
  173.                             pDPlay = NULL;
  174.  
  175.                             // Get pointer to the selected connection
  176.                             hr = GetConnection( hWnd, &pConnection );
  177.                             if( FAILED(hr) )
  178.                                 goto SP_FAILURE;
  179.  
  180.                             if( pConnection )
  181.                             {
  182.                                 hr = CreateDirectPlayInterface( &pDPlay );
  183.  
  184.                                 if( FAILED(hr) || NULL == pDPlay )
  185.                                     goto SP_FAILURE;
  186.  
  187.                                 // Initialize the connection
  188.                                 hr = pDPlay->InitializeConnection( pConnection, 0 );
  189.                                 if( FAILED(hr) )
  190.                                     goto SP_FAILURE;
  191.  
  192.                                 // OK to host now
  193.                                 EnableDlgButton( hWnd, IDC_HOSTBUTTON, TRUE );
  194.  
  195.                                 // Start enumerating the sessions
  196.                                 hr = EnumSessions( hWnd, pDPlay );
  197.                                 if( FAILED(hr) )
  198.                                     goto SP_FAILURE;
  199.  
  200.                                 // Set a timer to refresh the session list
  201.                                 idTimer = SetTimer( hWnd, TIMERID, TIMERINTERVAL, NULL );
  202.                             }
  203.                             else
  204.                             {
  205.                                 // They've selected the generic option "<Select
  206.                                 // a service provider>"
  207.                                 EnableDlgButton( hWnd, IDC_HOSTBUTTON, FALSE );
  208.                                 EnableDlgButton( hWnd, IDC_JOINBUTTON, FALSE );
  209.                             }
  210.                             break;
  211.                         }
  212.                         break;
  213.  
  214.                     SP_FAILURE:
  215.                         if( hr != DPERR_USERCANCEL )
  216.                             ErrorBox("Could not select service provider because of error %s", hr);
  217.                         break;
  218.  
  219.                     case IDC_HOSTBUTTON:
  220.                         // Should have an interface by now
  221.                         if( pDPlay == NULL )
  222.                             break;
  223.  
  224.                         if( idTimer )
  225.                         {
  226.                             KillTimer( hWnd, idTimer );
  227.                             idTimer = 0;
  228.                         }
  229.                         // Get server configuration from user
  230.                         ZeroMemory( &serverConfig, sizeof(SERVERCONFIG) );
  231.                         if( !GetServerConfig( hWnd, &serverConfig ) )
  232.                             break;
  233.  
  234.                         // Host a new session on this service provider
  235.                         hr = HostSession( pDPlay, &serverConfig, pDPInfo );
  236.                         if( FAILED(hr) )
  237.                             goto HOST_FAILURE;
  238.  
  239.                         // Dismiss dialog if we succeeded in hosting
  240.                         EndDialog( hWnd, TRUE );
  241.                         break;
  242.  
  243.                     HOST_FAILURE:
  244.                         ErrorBox("Could not host session because of error %s", hr);
  245.                         break;
  246.  
  247.                     case IDC_JOINBUTTON:
  248.                         // Should have an interface by now
  249.                         if( pDPlay == NULL )
  250.                             break;
  251.  
  252.                         if( idTimer )
  253.                         {
  254.                             KillTimer( hWnd, idTimer );
  255.                             idTimer = 0;
  256.                         }
  257.                         // Get guid of selected session instance
  258.                         hr = GetSessionInstanceGuid( hWnd, &guidSessionInstance );
  259.                         if( FAILED(hr) )
  260.                             goto JOIN_FAILURE;
  261.  
  262.                         // Get server configuration from user
  263.                         ZeroMemory( &clientLogin, sizeof(CLIENTLOGIN) );
  264.  
  265.                         // Use user name for player name
  266.                         dwNameSize = MAXSTRLEN;
  267.                         GetUserName( clientLogin.strPlayerName, &dwNameSize );
  268.                                     
  269.                         // Join this session
  270.                         hr = JoinSession( pDPlay, &guidSessionInstance,
  271.                                           &clientLogin, pDPInfo );
  272.  
  273.                         // Need to ask user for credentials
  274.                         if( hr == DPERR_LOGONDENIED )
  275.                         {
  276.                             if( !GetClientLogin( hWnd, &clientLogin ) )
  277.                                 break;
  278.  
  279.                             // Try again, this time with credentials
  280.                             hr = JoinSession( pDPlay, &guidSessionInstance,
  281.                                               &clientLogin, pDPInfo );
  282.                         }
  283.  
  284.                         if( FAILED(hr) )
  285.                             goto JOIN_FAILURE;
  286.  
  287.                         // Dismiss dialog if we succeeded in joining
  288.                         EndDialog( hWnd, TRUE );
  289.                         break;
  290.  
  291.                     JOIN_FAILURE:
  292.                         ErrorBox("Could not join session because of error %s", hr);
  293.                         break;
  294.  
  295.                 case IDCANCEL:
  296.                         if( idTimer )
  297.                         {
  298.                             KillTimer( hWnd, idTimer );
  299.                             idTimer = 0;
  300.                         }
  301.                         // Delete any interface created if cancelling
  302.                         hr = DestroyDirectPlayInterface( hWnd, pDPlay );
  303.                         pDPlay = NULL;
  304.  
  305.                         EndDialog( hWnd, FALSE );
  306.                         break;
  307.                         
  308.                 default:
  309.                     // Message was not handled 
  310.                     return FALSE;
  311.             }
  312.             break;
  313.             
  314.         default:
  315.             // Message was not handled 
  316.             return FALSE;
  317.     }
  318.  
  319.     // Message was handled 
  320.     return TRUE;
  321. }
  322.  
  323.  
  324.  
  325.  
  326. //-----------------------------------------------------------------------------
  327. // Name: DirectPlayEnumConnectionsCallback()
  328. // Desc:
  329. //-----------------------------------------------------------------------------
  330. BOOL FAR PASCAL DirectPlayEnumConnectionsCallback( const GUID* pguidSP,
  331.                            VOID* pConnection, DWORD dwConnectionSize,
  332.                            const DPNAME* pName, DWORD dwFlags, VOID* pContext )
  333. {
  334.     HWND    hWnd = (HWND)pContext;
  335.     LRESULT iIndex;
  336.     VOID*   pConnectionBuffer;
  337.  
  338.     // Store service provider name in combo box
  339.     iIndex = SendDlgItemMessage( hWnd, IDC_SPCOMBO, CB_ADDSTRING, 0, 
  340.                                  (LPARAM)pName->lpszShortNameA );
  341.     if( iIndex == CB_ERR )
  342.         return TRUE;
  343.  
  344.     // Make space for Connection Shortcut
  345.     pConnectionBuffer = GlobalAllocPtr( GHND, dwConnectionSize );
  346.     if( pConnectionBuffer == NULL )
  347.         return TRUE;
  348.  
  349.     // Store pointer to GUID in combo box
  350.     memcpy( pConnectionBuffer, pConnection, dwConnectionSize );
  351.     SendDlgItemMessage( hWnd, IDC_SPCOMBO, CB_SETITEMDATA, (WPARAM)iIndex, 
  352.                         (LPARAM)pConnectionBuffer );
  353.  
  354.     return TRUE;
  355. }
  356.  
  357.  
  358.  
  359.  
  360. //-----------------------------------------------------------------------------
  361. // Name: CreateDirectPlayInterface()
  362. // Desc:
  363. //-----------------------------------------------------------------------------
  364. HRESULT CreateDirectPlayInterface( LPDIRECTPLAY4A* ppDPlay )
  365. {
  366.     // Create an IDirectPlay interface
  367.     return CoCreateInstance( CLSID_DirectPlay, NULL, CLSCTX_INPROC_SERVER,
  368.                              IID_IDirectPlay4A, (VOID**)ppDPlay );
  369. }
  370.  
  371.  
  372.  
  373.  
  374. //-----------------------------------------------------------------------------
  375. // Name: DestroyDirectPlayInterface()
  376. // Desc:
  377. //-----------------------------------------------------------------------------
  378. HRESULT DestroyDirectPlayInterface( HWND hWnd, LPDIRECTPLAY4A pDPlay )
  379. {
  380.     if( NULL == pDPlay )
  381.         return DP_OK;
  382.  
  383.     DeleteSessionInstanceList( hWnd );
  384.     EnableDlgButton( hWnd, IDC_JOINBUTTON, FALSE );
  385.  
  386.     return pDPlay->Release();
  387. }
  388.  
  389.  
  390.  
  391.  
  392. //-----------------------------------------------------------------------------
  393. // Name: HostSession()
  394. // Desc:
  395. //-----------------------------------------------------------------------------
  396. HRESULT HostSession( LPDIRECTPLAY4A pDPlay, SERVERCONFIG* pServerConfig,
  397.                      DPLAYINFO* pDPInfo )
  398. {
  399.     DPID            dpidPlayer;
  400.     DPSESSIONDESC2  sessionDesc;
  401.     DPSECURITYDESC  securityDesc;
  402.     DPSECURITYDESC* pSecurityDesc;
  403.     HRESULT         hr;
  404.  
  405.     // Check for valid interface
  406.     if( pDPlay == NULL)
  407.         return DPERR_INVALIDOBJECT;
  408.  
  409.     // Host a new session
  410.     ZeroMemory(&sessionDesc, sizeof(DPSESSIONDESC2));
  411.     sessionDesc.dwSize  = sizeof(DPSESSIONDESC2);
  412.     sessionDesc.dwFlags = SESSIONFLAGS( pServerConfig->bRequireSecureLogin );
  413.     sessionDesc.guidApplication  = DPSLOTS_GUID;
  414.     sessionDesc.dwMaxPlayers     = 0;
  415.     sessionDesc.lpszSessionNameA = pServerConfig->strServerName;
  416.  
  417.     // Assume no security descriptor will be needed
  418.     pSecurityDesc = NULL;
  419.  
  420.     if( pServerConfig->bRequireSecureLogin )
  421.     {
  422.         // A service provider string was entered, so use it
  423.         if( lstrlen( pServerConfig->strSecurityProvider ) )
  424.         {
  425.             ZeroMemory(&securityDesc, sizeof(DPSECURITYDESC));
  426.             securityDesc.dwSize = sizeof(DPSECURITYDESC);
  427.             securityDesc.dwFlags = 0;
  428.             securityDesc.lpszSSPIProviderA = pServerConfig->strSecurityProvider;
  429.             pSecurityDesc = &securityDesc;
  430.         }
  431.     }
  432.  
  433.     // Host the session
  434.     hr = pDPlay->SecureOpen( &sessionDesc, DPOPEN_CREATE, pSecurityDesc, NULL );
  435.     if( FAILED(hr) )
  436.         goto FAILURE;
  437.  
  438.     // Create a server player with no name
  439.     hr = pDPlay->CreatePlayer( &dpidPlayer, NULL, pDPInfo->hPlayerEvent, NULL,
  440.                                0, SERVERPLAYERFLAGS );
  441.     if( FAILED(hr) )
  442.         goto FAILURE;
  443.  
  444.     // Return connection info
  445.     pDPInfo->pDPlay     = pDPlay;
  446.     pDPInfo->dpidPlayer = dpidPlayer;
  447.     pDPInfo->bIsHost    = TRUE;
  448.     pDPInfo->bIsSecure  = pServerConfig->bRequireSecureLogin;
  449.  
  450.     // Save database name in global
  451.     lstrcpy( g_strDatabaseName, pServerConfig->strDatabaseFile );
  452.  
  453.     return DP_OK;
  454.  
  455. FAILURE:
  456.     pDPlay->Close();
  457.     return hr;
  458. }
  459.  
  460.  
  461.  
  462.  
  463. //-----------------------------------------------------------------------------
  464. // Name: JoinSession()
  465. // Desc:
  466. //-----------------------------------------------------------------------------
  467. HRESULT JoinSession( LPDIRECTPLAY4A pDPlay, GUID* pSessionInstanceGUID,
  468.                      CLIENTLOGIN* pClientLogin, DPLAYINFO* pDPInfo )
  469. {
  470.     DPID            dpidPlayer;
  471.     DPNAME          dpName;
  472.     DPSESSIONDESC2  sessionDesc;
  473.     DPSESSIONDESC2* pSessionDesc = NULL;
  474.     DPCREDENTIALS   credentials;
  475.     DPCREDENTIALS*  pCredentials;
  476.     HRESULT         hr;
  477.  
  478.     // Check for valid interface
  479.     if( pDPlay == NULL)
  480.         return DPERR_INVALIDOBJECT;
  481.  
  482.     // Join existing session
  483.     ZeroMemory( &sessionDesc, sizeof(DPSESSIONDESC2) );
  484.     sessionDesc.dwSize = sizeof(DPSESSIONDESC2);
  485.     sessionDesc.guidInstance = *pSessionInstanceGUID;
  486.  
  487.     // Assume no credentials are going to be used
  488.     pCredentials = NULL;
  489.  
  490.     // Setup credentials
  491.     ZeroMemory( &credentials, sizeof(DPCREDENTIALS) );
  492.     credentials.dwSize = sizeof(DPCREDENTIALS);
  493.     credentials.dwFlags = 0;
  494.  
  495.     if( lstrlen( pClientLogin->strUserName ) )
  496.     {
  497.         credentials.lpszUsernameA = pClientLogin->strUserName;
  498.         pCredentials = &credentials;
  499.     }
  500.  
  501.     if( lstrlen( pClientLogin->strPassword ) )
  502.     {
  503.         credentials.lpszPasswordA = pClientLogin->strPassword; 
  504.         pCredentials = &credentials;
  505.     }
  506.  
  507.     if( lstrlen( pClientLogin->strDomain ) )
  508.     {
  509.         credentials.lpszDomainA = pClientLogin->strDomain; 
  510.         pCredentials = &credentials;
  511.     }
  512.  
  513.     // Join the session 
  514.     hr = pDPlay->SecureOpen( &sessionDesc, DPOPEN_JOIN, NULL, pCredentials );
  515.     
  516.     if( FAILED(hr) )
  517.         goto FAILURE;
  518.  
  519.     // Fill out name structure
  520.     ZeroMemory( &dpName, sizeof(DPNAME) );
  521.     dpName.dwSize = sizeof(DPNAME);
  522.     dpName.lpszShortNameA = pClientLogin->strPlayerName;
  523.     dpName.lpszLongNameA = NULL;
  524.  
  525.     // Create a player with this name
  526.     hr = pDPlay->CreatePlayer( &dpidPlayer, &dpName, pDPInfo->hPlayerEvent,
  527.                                NULL, 0, CLIENTPLAYERFLAGS );
  528.     if( FAILED(hr) )
  529.         goto FAILURE;
  530.  
  531.     // Get the session desc
  532.     hr = GetSessionDesc( pDPlay, &pSessionDesc );
  533.     if( FAILED(hr) )
  534.         goto FAILURE;
  535.  
  536.     // Return connection info
  537.     pDPInfo->pDPlay     = pDPlay;
  538.     pDPInfo->dpidPlayer = dpidPlayer;
  539.     pDPInfo->bIsHost    = FALSE;
  540.  
  541.     if( pSessionDesc->dwFlags & DPSESSION_SECURESERVER )
  542.         pDPInfo->bIsSecure = TRUE;
  543.     else
  544.         pDPInfo->bIsSecure = FALSE;
  545.  
  546.     GlobalFreePtr( pSessionDesc );
  547.  
  548.     return DP_OK;
  549.  
  550. FAILURE:
  551.     if( pSessionDesc )
  552.         GlobalFreePtr( pSessionDesc );
  553.  
  554.     pDPlay->Close();
  555.     return hr;
  556. }
  557.  
  558.  
  559.  
  560.  
  561. //-----------------------------------------------------------------------------
  562. // Name: EnumSessionsCallback()
  563. // Desc:
  564. //-----------------------------------------------------------------------------
  565. BOOL FAR PASCAL EnumSessionsCallback( const DPSESSIONDESC2* pSessionDesc,
  566.                                       DWORD* pdwTimeOut, DWORD dwFlags,
  567.                                       VOID* pContext )
  568. {
  569.     HWND  hWnd = (HWND)pContext;
  570.     GUID* pGuid;
  571.     LONG  iIndex;
  572.  
  573.     // See if last session has been enumerated
  574.     if( dwFlags & DPESC_TIMEDOUT )
  575.         return FALSE;
  576.  
  577.     // Store session name in list
  578.     iIndex = SendDlgItemMessage( hWnd, IDC_SESSIONLIST, LB_ADDSTRING, 
  579.                                  0, (LPARAM)pSessionDesc->lpszSessionNameA );
  580.     if( iIndex == LB_ERR )
  581.         return TRUE;
  582.  
  583.     // Make space for session instance guid
  584.     pGuid = (GUID*)GlobalAllocPtr( GHND, sizeof(GUID) );
  585.     if( pGuid == NULL )
  586.         return TRUE;
  587.  
  588.     // Store pointer to guid in list
  589.     *pGuid = pSessionDesc->guidInstance;
  590.     SendDlgItemMessage( hWnd, IDC_SESSIONLIST, LB_SETITEMDATA,
  591.                         (WPARAM)iIndex, (LPARAM)pGuid );
  592.  
  593.     return TRUE;
  594. }
  595.  
  596.  
  597.  
  598.  
  599. //-----------------------------------------------------------------------------
  600. // Name: EnumSessions()
  601. // Desc:
  602. //-----------------------------------------------------------------------------
  603. HRESULT EnumSessions( HWND hWnd, LPDIRECTPLAY4A pDPlay )
  604. {
  605.     DPSESSIONDESC2 sessionDesc;
  606.     GUID           guidSessionInstance;
  607.     LONG           iIndex;
  608.     HRESULT        hr;
  609.  
  610.     // Check for valid interface
  611.     if( pDPlay == NULL)
  612.         return DPERR_INVALIDOBJECT;
  613.  
  614.     // Get guid of currently selected session
  615.     guidSessionInstance = GUID_NULL;
  616.     hr = GetSessionInstanceGuid( hWnd, &guidSessionInstance );
  617.  
  618.     // Delete existing session list
  619.     DeleteSessionInstanceList( hWnd );
  620.  
  621.     // Add sessions to session list
  622.     ZeroMemory( &sessionDesc, sizeof(DPSESSIONDESC2) );
  623.     sessionDesc.dwSize = sizeof(DPSESSIONDESC2);
  624.     sessionDesc.guidApplication = DPSLOTS_GUID;
  625.  
  626.     hr = pDPlay->EnumSessions( &sessionDesc, 0, EnumSessionsCallback,
  627.                                hWnd,
  628.                                DPENUMSESSIONS_AVAILABLE|DPENUMSESSIONS_ASYNC );
  629.  
  630.     // Select the session that was previously selected
  631.     SelectSessionInstance( hWnd, &guidSessionInstance );
  632.  
  633.     // Hilite "Join" button only if there are sessions to join
  634.     iIndex = SendDlgItemMessage( hWnd, IDC_SESSIONLIST, LB_GETCOUNT, 0, 0 );
  635.  
  636.     EnableDlgButton( hWnd, IDC_JOINBUTTON, (iIndex > 0) ? TRUE : FALSE );
  637.  
  638.     return hr;
  639. }
  640.  
  641.  
  642.  
  643.  
  644. //-----------------------------------------------------------------------------
  645. // Name: GetConnection()
  646. // Desc:
  647. //-----------------------------------------------------------------------------
  648. HRESULT GetConnection( HWND hWnd, VOID** ppConnection )
  649. {
  650.     LONG iIndex;
  651.  
  652.     // get index of the item currently selected in the combobox
  653.     iIndex = SendDlgItemMessage( hWnd, IDC_SPCOMBO, CB_GETCURSEL, 0, 0 );
  654.     if( iIndex == CB_ERR )
  655.         return DPERR_GENERIC;
  656.  
  657.     // Get the pointer to the connection shortcut associated with
  658.     // the item
  659.     iIndex = SendDlgItemMessage( hWnd, IDC_SPCOMBO, CB_GETITEMDATA,
  660.                                  (WPARAM)iIndex, 0 );
  661.     if( iIndex == CB_ERR )
  662.         return DPERR_GENERIC;
  663.  
  664.     *ppConnection = (VOID*)iIndex;
  665.  
  666.     return DP_OK;
  667. }
  668.  
  669.  
  670.  
  671.  
  672. //-----------------------------------------------------------------------------
  673. // Name: DeleteConnectionList()
  674. // Desc:
  675. //-----------------------------------------------------------------------------
  676. VOID DeleteConnectionList( HWND hWnd )
  677. {
  678.     WPARAM i;
  679.     LONG   pData;
  680.     
  681.     // Destroy the GUID's stored with each service provider name
  682.     i = 0;
  683.     while( TRUE )
  684.     {
  685.         // Get data pointer stored with item
  686.         pData = SendDlgItemMessage( hWnd, IDC_SPCOMBO, CB_GETITEMDATA,
  687.                                     (WPARAM) i, 0 );
  688.         if( pData == CB_ERR )
  689.             break;
  690.  
  691.         if( pData != 0)
  692.             GlobalFreePtr( (VOID*)pData );
  693.  
  694.         i += 1;
  695.     }
  696.  
  697.     // Delete all items in combo box
  698.     SendDlgItemMessage( hWnd, IDC_SPCOMBO, CB_RESETCONTENT, 0, 0 );
  699. }
  700.  
  701.  
  702.  
  703.  
  704. //-----------------------------------------------------------------------------
  705. // Name: GetSessionInstanceGuid()
  706. // Desc:
  707. //-----------------------------------------------------------------------------
  708. HRESULT GetSessionInstanceGuid( HWND hWnd, GUID* pguidSessionInstance )
  709. {
  710.     LONG iIndex;
  711.  
  712.     // get guid for session
  713.     iIndex = SendDlgItemMessage( hWnd, IDC_SESSIONLIST, LB_GETCURSEL, 0, 0 );
  714.     if( iIndex == LB_ERR )
  715.         return DPERR_GENERIC;
  716.  
  717.     iIndex = SendDlgItemMessage( hWnd, IDC_SESSIONLIST, LB_GETITEMDATA,
  718.                                  (WPARAM)iIndex, 0 );
  719.     if( (iIndex == LB_ERR) || (iIndex == 0) )
  720.         return DPERR_GENERIC;
  721.  
  722.     *pguidSessionInstance = *((GUID*)iIndex);
  723.  
  724.     return DP_OK;
  725. }
  726.  
  727.  
  728.  
  729.  
  730. //-----------------------------------------------------------------------------
  731. // Name: DeleteSessionInstanceList()
  732. // Desc:
  733. //-----------------------------------------------------------------------------
  734. VOID DeleteSessionInstanceList( HWND hWnd )
  735. {
  736.     WPARAM i;
  737.     LONG   pData;
  738.     
  739.     // Destroy the GUID's stored with each session name
  740.     i = 0;
  741.     while( TRUE )
  742.     {
  743.         // get data pointer stored with item
  744.         pData = SendDlgItemMessage( hWnd, IDC_SESSIONLIST, LB_GETITEMDATA,
  745.                                     (WPARAM) i, (LPARAM)0 );
  746.         if( pData == CB_ERR )
  747.             break;
  748.         if( pData == 0 )
  749.             continue;
  750.  
  751.         GlobalFreePtr( (VOID*)pData );
  752.         i += 1;
  753.     }
  754.  
  755.     // Delete all items in list
  756.     SendDlgItemMessage( hWnd, IDC_SESSIONLIST, LB_RESETCONTENT, 0, 0 );
  757. }
  758.  
  759.  
  760.  
  761.  
  762. //-----------------------------------------------------------------------------
  763. // Name: SelectSessionInstance()
  764. // Desc:
  765. //-----------------------------------------------------------------------------
  766. VOID SelectSessionInstance( HWND hWnd, GUID* pguidSessionInstance )
  767. {
  768.     WPARAM i, iIndex;
  769.     LONG   pData;
  770.     
  771.     // Loop over the GUID's stored with each session name
  772.     // to find the one that matches what was passed in
  773.     i = 0;
  774.     iIndex = 0;
  775.     while( TRUE )
  776.     {
  777.         // Get data pointer stored with item
  778.         pData = SendDlgItemMessage( hWnd, IDC_SESSIONLIST, LB_GETITEMDATA,
  779.                                     (WPARAM)i, 0 );
  780.         if( pData == CB_ERR)
  781.             break;
  782.  
  783.         if( pData == 0)
  784.             continue;
  785.  
  786.         // Guid matches
  787.         if( IsEqualGUID( *pguidSessionInstance, *((GUID*)pData ) ) )
  788.         {
  789.             iIndex = i;
  790.             break;
  791.         }
  792.  
  793.         i += 1;
  794.     }
  795.  
  796.     // Select this item
  797.     SendDlgItemMessage( hWnd, IDC_SESSIONLIST, LB_SETCURSEL, (WPARAM)iIndex, 0 );
  798. }
  799.  
  800.  
  801.  
  802.  
  803. //-----------------------------------------------------------------------------
  804. // Name: GetSessionDesc()
  805. // Desc:
  806. //-----------------------------------------------------------------------------
  807. HRESULT GetSessionDesc( LPDIRECTPLAY4A pDPlay, DPSESSIONDESC2** ppSessionDesc )
  808. {
  809.     DPSESSIONDESC2* pSessionDesc = NULL;
  810.     DWORD           dwSessionDescSize;
  811.     HRESULT         hr;
  812.  
  813.     // Get size of session desc
  814.     hr = pDPlay->GetSessionDesc( NULL, &dwSessionDescSize );
  815.     if( hr != DPERR_BUFFERTOOSMALL )
  816.         goto FAILURE;
  817.  
  818.     // Make room for it
  819.     pSessionDesc = (DPSESSIONDESC2*)GlobalAllocPtr( GHND, dwSessionDescSize );
  820.     if( pSessionDesc == NULL )
  821.     {
  822.         hr = DPERR_OUTOFMEMORY;
  823.         goto FAILURE;
  824.     }
  825.  
  826.     // Get the session desc
  827.     hr = pDPlay->GetSessionDesc( pSessionDesc, &dwSessionDescSize );
  828.     if( FAILED(hr) )
  829.         goto FAILURE;
  830.  
  831.     // Return account description
  832.     *ppSessionDesc = pSessionDesc;
  833.  
  834.     return S_OK;
  835.  
  836. FAILURE:
  837.     if( pSessionDesc )
  838.         GlobalFreePtr( pSessionDesc );
  839.  
  840.     return hr;
  841. }
  842.  
  843.  
  844.  
  845.  
  846. //-----------------------------------------------------------------------------
  847. // Name: InitializeServerConfigWindow()
  848. // Desc:
  849. //-----------------------------------------------------------------------------
  850. VOID InitializeServerConfigWindow( HWND hWnd )
  851. {
  852.     CHAR  strSessionName[MAXSTRLEN];
  853.     DWORD dwNameSize = MAXSTRLEN;
  854.  
  855.     // Use username for default
  856.     if( GetComputerName( strSessionName, &dwNameSize ) )
  857.         SetDlgItemText( hWnd, IDC_SERVERNAMEEDIT, strSessionName );
  858.     
  859.     // Use default name
  860.     SetDlgItemText( hWnd, IDC_DATABASEFILEEDIT, DEFAULTDATABASE );
  861.  
  862.     // Security off by default
  863.     CheckDlgItem( hWnd, IDC_SECURECHECK, FALSE );
  864. }
  865.  
  866.  
  867.  
  868.  
  869. //-----------------------------------------------------------------------------
  870. // Name: SaveServerConfig() 
  871. // Desc:
  872. //-----------------------------------------------------------------------------
  873. VOID SaveServerConfig( HWND hWnd, SERVERCONFIG* pServerConfig )
  874. {
  875.     // Get strings from dialog
  876.     GetDlgItemText( hWnd, IDC_SERVERNAMEEDIT, pServerConfig->strServerName, MAXSTRLEN );
  877.     GetDlgItemText( hWnd, IDC_DATABASEFILEEDIT, pServerConfig->strDatabaseFile, MAXSTRLEN );
  878.     GetDlgItemText( hWnd, IDC_SECURITYPROVIDEREDIT, pServerConfig->strSecurityProvider, MAXSTRLEN );
  879.  
  880.     if( DlgItemIsChecked(hWnd, IDC_SECURECHECK) )
  881.         pServerConfig->bRequireSecureLogin = TRUE;
  882.     else
  883.         pServerConfig->bRequireSecureLogin = FALSE;
  884. }
  885.  
  886.  
  887.  
  888.  
  889. //-----------------------------------------------------------------------------
  890. // Name: ServerConfigWndProc()
  891. // Desc:
  892. //-----------------------------------------------------------------------------
  893. BOOL CALLBACK ServerConfigWndProc( HWND hWnd, UINT uMsg, WPARAM wParam,
  894.                                    LPARAM lParam )
  895. {
  896.     static SERVERCONFIG* pServerConfig;
  897.  
  898.     switch( uMsg )
  899.     {
  900.         case WM_INITDIALOG:
  901.             // Globals are passed in lParam
  902.             pServerConfig = (SERVERCONFIG*)lParam;
  903.  
  904.             // Initialize dialog with appropriate information
  905.             InitializeServerConfigWindow( hWnd );
  906.             break;
  907.  
  908.         case WM_DESTROY:
  909.             break;
  910.  
  911.         case WM_COMMAND:
  912.             switch( LOWORD(wParam) )
  913.             {
  914.                 case IDOK:
  915.                     // save changes they made
  916.                     SaveServerConfig( hWnd, pServerConfig );
  917.             
  918.                     // Return success
  919.                     EndDialog( hWnd, TRUE );
  920.                     break;
  921.  
  922.                 case IDCANCEL:
  923.                     // Return failure
  924.                     EndDialog( hWnd, FALSE );
  925.                     break;
  926.             }
  927.             break;
  928.     }
  929.  
  930.     // Allow for default processing
  931.     return FALSE;
  932. }
  933.  
  934.  
  935.  
  936.  
  937. //-----------------------------------------------------------------------------
  938. // Name: GetServerConfig()
  939. // Desc:
  940. //-----------------------------------------------------------------------------
  941. BOOL GetServerConfig( HWND hWnd, SERVERCONFIG* pServerConfig )
  942. {
  943.     // Collect server config from dialog
  944.     return DialogBoxParam( ghInstance, MAKEINTRESOURCE(IDD_SERVERCONFIGDIALOG),
  945.                            hWnd, (DLGPROC)ServerConfigWndProc,
  946.                            (LPARAM)pServerConfig );
  947. }
  948.  
  949.  
  950.  
  951.  
  952. //-----------------------------------------------------------------------------
  953. // Name: InitializeClientLoginWindow()
  954. // Desc:
  955. //-----------------------------------------------------------------------------
  956. VOID InitializeClientLoginWindow( HWND hWnd )
  957. {
  958.     CHAR  strPlayerName[MAXSTRLEN];
  959.     DWORD dwNameSize;
  960.  
  961.     // Use user name for player name
  962.     dwNameSize = MAXSTRLEN;
  963.     if( GetUserName( strPlayerName, &dwNameSize ) )
  964.         SetDlgItemText( hWnd, IDC_USERNAMEEDIT, strPlayerName );
  965. }
  966.  
  967.  
  968.  
  969.  
  970. //-----------------------------------------------------------------------------
  971. // Name: SaveClientLogin()
  972. // Desc:
  973. //-----------------------------------------------------------------------------
  974. VOID SaveClientLogin( HWND hWnd, CLIENTLOGIN* pClientLogin )
  975. {
  976.     // Get strings from dialog
  977.     GetDlgItemText( hWnd, IDC_USERNAMEEDIT, pClientLogin->strUserName, MAXSTRLEN );
  978.     GetDlgItemText( hWnd, IDC_PASSWORDEDIT, pClientLogin->strPassword, MAXSTRLEN );
  979.     GetDlgItemText( hWnd, IDC_DOMAINEDIT,   pClientLogin->strDomain,   MAXSTRLEN );
  980. }
  981.  
  982.  
  983.  
  984.  
  985. //-----------------------------------------------------------------------------
  986. // Name: ClientLoginWndProc()
  987. // Desc:
  988. //-----------------------------------------------------------------------------
  989. BOOL CALLBACK ClientLoginWndProc( HWND hWnd, UINT uMsg, WPARAM wParam,
  990.                                   LPARAM lParam)
  991. {
  992.     static CLIENTLOGIN* pClientLogin;
  993.  
  994.     switch( uMsg )
  995.     {
  996.         case WM_INITDIALOG:
  997.             // Globals are passed in lParam
  998.             pClientLogin = (CLIENTLOGIN*)lParam;
  999.  
  1000.             // Initialize dialog with appropriate information
  1001.             InitializeClientLoginWindow( hWnd );
  1002.             break;
  1003.  
  1004.         case WM_DESTROY:
  1005.             break;
  1006.  
  1007.         case WM_COMMAND:
  1008.             switch( LOWORD(wParam) )
  1009.             {
  1010.                 case IDOK:
  1011.                     // save changes they made
  1012.                     SaveClientLogin( hWnd, pClientLogin );
  1013.                     // Return success
  1014.                     EndDialog( hWnd, TRUE );
  1015.                     break;
  1016.  
  1017.                 case IDCANCEL:
  1018.                     // Return failure
  1019.                     EndDialog( hWnd, FALSE );
  1020.                     break;
  1021.             }
  1022.             break;
  1023.     }
  1024.  
  1025.     // Allow for default processing
  1026.     return FALSE;
  1027. }
  1028.  
  1029.  
  1030.  
  1031.  
  1032. //-----------------------------------------------------------------------------
  1033. // Name: GetClientLogin()
  1034. // Desc:
  1035. //-----------------------------------------------------------------------------
  1036. BOOL GetClientLogin( HWND hWnd, CLIENTLOGIN* pClientLogin )
  1037. {
  1038.     // Collect server config from dialog
  1039.     return DialogBoxParam( ghInstance, MAKEINTRESOURCE(IDD_CLIENTLOGINDIALOG),
  1040.                            hWnd, (DLGPROC)ClientLoginWndProc,
  1041.                            (LPARAM)pClientLogin );
  1042. }
  1043.  
  1044.  
  1045.  
  1046.